﻿Imports System.Data.OleDb

Public Class Details
    Inherits System.Web.UI.Page

    Private Sub Details_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim sqlRemove = "INSERT INTO ParrotsViewed (ParrotID_FK, UserID_FK) VALUES (@F1, @F2)"
        Dim cmdRem = New OleDbCommand(sqlRemove, conn)
        conn.Open()
        cmdRem.Parameters.AddWithValue("@F1", Request.Params("ParrotID"))
        cmdRem.Parameters.AddWithValue("@F2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmdRem.ExecuteNonQuery()
        conn.Close()

    End Sub

    Protected Sub btn_comment_Click(sender As Object, e As EventArgs) Handles btn_comment.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim sql = "INSERT INTO Comment (Author,Subject,Message,ParrotID) VALUES (@F1, @F2, @F3, @F4)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@F1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@F2", tb_subject.Text)
        cmd.Parameters.AddWithValue("@F3", tb_message.Text)
        cmd.Parameters.AddWithValue("@F4", Request.Params("ParrotID"))
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("Details.aspx?ParrotID=" & Request.Params("ParrotID"))
    End Sub


    Protected Sub btn_Rate_Click(sender As Object, e As EventArgs) Handles btn_Rate.Click

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim sqlRemove = "DELETE FROM ParrotRatings WHERE UserID_FK=@F1 AND ParrotID_FK=@f2"
        Dim cmdRem = New OleDbCommand(sqlRemove, conn)
        conn.Open()
        cmdRem.Parameters.AddWithValue("@F1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmdRem.Parameters.AddWithValue("@F2", Request.Params("ParrotID"))
        cmdRem.ExecuteNonQuery()
        conn.Close()

        Dim sqlAdd = "INSERT INTO ParrotRatings (ParrotRating,ParrotID_FK,UserID_FK) VALUES (@F1, @F2, @F3)"
        Dim cmd = New OleDbCommand(sqlAdd, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@F1", ParrotRating.CurrentRating)
        cmd.Parameters.AddWithValue("@F2", Request.Params("ParrotID"))
        cmd.Parameters.AddWithValue("@F3", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.ExecuteNonQuery()
        conn.Close()

    End Sub

  

    Protected Sub btn_ApplyTags_Click(sender As Object, e As EventArgs) Handles btn_ApplyTags.Click
        Dim oledbConn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim sql = "INSERT INTO ParrotTag_Junction (ParrotID_FK,TagID_FK) VALUES (@F1, @F2)"
        oledbConn.Open()
        For Each check As ListItem In cbl_TagSearch.Items
            If check.Selected Then
                Dim cmd = New OleDbCommand(sql, oledbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@F1", Request.Params("ParrotID"))
                cmd.Parameters.AddWithValue("@F2", check.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        If tb_NewTags.Text = "" Then Return

        Dim newTags = tb_NewTags.Text.Split(",")
        For Each newTag In newTags

            'Inserting the new tag into the Tags Table

            Dim sql_newTag = "INSERT INTO Tags(TagName) VALUES(@F1)"
            Dim cmd As OleDbCommand = New OleDbCommand(sql_newTag, oledbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'Finding ID of the newly Added Tag
            Dim getLastIDCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 TagID from Tags ORDER BY TagID DESC", oledbConn)
            Dim dr = getLastIDCmd.ExecuteReader()
            dr.Read()
            Dim lastId = dr.GetValue(0)

            'Associating the found newTag ID with the ParrotID

            Dim Newcmd = New OleDbCommand(sql, oledbConn)
            Newcmd.CommandType = CommandType.Text
            Newcmd.Parameters.AddWithValue("@F1", Request.Params("ParrotID"))
            Newcmd.Parameters.AddWithValue("@F2", lastId)
            Newcmd.ExecuteNonQuery()

        Next
        Response.Redirect("Details.aspx?ParrotID=" & Request.Params("ParrotID"))
    End Sub
End Class

